package com.jy.appmarket.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class Prefs {
	private static SharedPreferences mInstance = null;

	// item tag
	private static final String DESKTOP_SOFTWARE_LSIT = "desktopSoftware";
	private static final String DESKTOP_GAME_LIST = "desktopGame";
	private static final String DESKTOP_ENTERTAINMENT_LIST = "desktopEntertainment";

	private static final String ENTER_TOOL_WIDGET = "enter_tool_widget";
	private static final String ENTER_GAME_WIDGET = "enter_game_widget";
	private static final String ENTER_ENTERTAINMENT_WIDGET = "enter_entertainment_widget";
	private static final String ENTER_NECESSARIES_WIDGET = "enter_necessaries_widget";
	private static final String INIT_DEFAULT_PACKAGE = "init_default_package";

	private static final String ITEM_UPGRADE_CARD_DATE = "upgradeCardDate";

	private static final String ITEM_UPGRADE_VERSION_CODE = "upgradeVersionCode";
	private static final String ITEM_UPGRADE_ICON = "upgradeIcon";
	private static final String ITEM_UPGRADE_TITLE = "upgradeTitle";
	private static final String ITEM_UPGRADE_DETAIL = "upgradeDetail";
	private static final String ITEM_UPGRADE_URL = "upgradeUrl";
	private static final String ITEM_UPGRADE_INDEX = "upgradeIndex";

	private static final String ITEM_UPGRADE_NOTIFY_NOON = "upgradeNotifyNoon";
	private static final String ITEM_UPGRADE_NOTIFY_NIGHT = "upgradeNotifyNight";
	private static final String ITEM_SEARCHSHORTCUT_FLAG = "searchshortcutflag";

	// 应用更新检测的时间
	private static final String ITEM_APPUPDATE_DATE = "appupdatedate"; // YYYY-MM-DD

	// 下一次应用更新检测时间
	private static final String ITEM_APPUPDATE_NEXT_QUERY_DATE = "appupdatenextquerydate"; // YYYY-MM-DD

	// 应用更新中午通知日期
	private static final String ITEM_APPUPDATE_NOTIFICATION_NOON = "appupdatenotificationnoon"; // YYYY-MM-DD
	// 应用更新晚上通知日期
	private static final String ITEM_APPUPDATE_NOTIFICATION_NIGHT = "appupdatenotificationnight"; //

	private static final String ITEM_SHOW_APP_UPDATE_IGNORE_DIALOG_FLAG = "appupdateignoredialog";

	private static final String ITEM_ACTIVITE_STATIC_COUNT = "activitestaticcount"; // 激活统计次数

	// 登录信息
	private static final String ITEM_USER_ID = "userId";
	private static final String ITEM_USER_PHONE_NUM = "userPhoneNum";
	private static final String ITEM_USER_ICON_URL = "userIconUrl";
	private static final String ITEM_USER_NICK_NAME = "userNickName";
	private static final String ITEM_USER_SEX = "userSex";
	private static final String ITEM_USER_BIRTHDAY = "userBirthday";

	// double check and lock
	private static SharedPreferences getInstance(Context ctx) {
		if (mInstance == null) {
			synchronized (Prefs.class) {
				if (mInstance == null) {
					mInstance = PreferenceManager.getDefaultSharedPreferences(ctx);
				}
			}
		}
		return mInstance;
	}

	private static SharedPreferences get(Context ctx) {
		return getInstance(ctx);
	}

	private static String removeSameItem(String list) {
		if (list == null || list.length() == 0) {
			return list;
		}
		String[] split = list.split("\\|");
		List<String> newList = new ArrayList<String>();
		for (String s : split) {
			if (!newList.contains(s)) {
				newList.add(s);
			}
		}
		if (newList.size() == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (String s : newList) {
			sb.append(s).append("|");
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * 桌面文件夹中的工具list
	 */

	public static void saveDesktopSoftware(Context c, String list) {
		SharedPreferences sp = get(c);
		sp.edit().putString(DESKTOP_SOFTWARE_LSIT, removeSameItem(list)).commit();
	}

	public static String getDesktopSoftware(Context c) {
		return get(c).getString(DESKTOP_SOFTWARE_LSIT, "");
	}

	/**
	 * 桌面文件夹中的游戏list
	 */

	public static void saveDesktopGame(Context c, String list) {
		SharedPreferences sp = get(c);
		sp.edit().putString(DESKTOP_GAME_LIST, removeSameItem(list)).commit();
	}

	public static String getDesktopGame(Context c) {
		return get(c).getString(DESKTOP_GAME_LIST, "");
	}

	public static void saveDesktopEntertainment(Context c, String list) {
		SharedPreferences sp = get(c);
		sp.edit().putString(DESKTOP_ENTERTAINMENT_LIST, removeSameItem(list)).commit();
	}

	public static String getDesktopEntertainment(Context c) {
		return get(c).getString(DESKTOP_ENTERTAINMENT_LIST, "");
	}

	public static boolean hasEnterToolWidget(Context c) {
		return get(c).getBoolean(ENTER_TOOL_WIDGET, false);
	}

	public static void setEnterToolWidget(Context c, boolean enter) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(ENTER_TOOL_WIDGET, enter).commit();
	}

	public static boolean hasEnterGameWidget(Context c) {
		return get(c).getBoolean(ENTER_GAME_WIDGET, false);
	}

	public static void setEnterGameWidget(Context c, boolean enter) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(ENTER_GAME_WIDGET, enter).commit();
	}

	public static boolean hasEnterEntertainmentWidget(Context c) {
		return get(c).getBoolean(ENTER_ENTERTAINMENT_WIDGET, false);
	}

	public static void setEnterEntertainmentWidget(Context c, boolean enter) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(ENTER_ENTERTAINMENT_WIDGET, enter).commit();
	}

	public static boolean hasEnterNecessariesWidget(Context c) {
		return get(c).getBoolean(ENTER_NECESSARIES_WIDGET, false);
	}

	public static void setEnterNecessariesWidget(Context c, boolean enter) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(ENTER_NECESSARIES_WIDGET, enter).commit();
	}

	public static boolean hasInitDefaultPackage(Context c) {
		return get(c).getBoolean(INIT_DEFAULT_PACKAGE, false);
	}

	public static void setInitDefaultPackage(Context c, boolean init) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(INIT_DEFAULT_PACKAGE, init).commit();
	}

	public static String getUpgradeCardDate(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_CARD_DATE, "");
	}

	public static void saveUpgradeCardDate(Context c, String date) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_CARD_DATE, date).commit();
	}

	public static int getUpgradeVersionCode(Context c) {
		SharedPreferences p = get(c);
		return p.getInt(ITEM_UPGRADE_VERSION_CODE, 0);
	}

	public static void saveUpgradeVersionCode(Context c, int versionCode) {
		SharedPreferences sp = get(c);
		sp.edit().putInt(ITEM_UPGRADE_VERSION_CODE, versionCode).commit();
	}

	public static String getUpgradeIcon(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_ICON, "");
	}

	public static void saveUpgradeIcon(Context c, String icon) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_ICON, icon).commit();
	}

	public static String getUpgradeTitle(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_TITLE, "");
	}

	public static void saveUpgradeTitle(Context c, String title) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_TITLE, title).commit();
	}

	public static String getUpgradeDetail(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_DETAIL, "");
	}

	public static void saveUpgradeDetail(Context c, String detail) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_DETAIL, detail).commit();
	}

	public static String getUpgradeUrl(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_URL, "");
	}

	public static void saveUpgradeUrl(Context c, String url) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_URL, url).commit();
	}

	public static int getUpgradeIndex(Context c) {
		SharedPreferences p = get(c);
		return p.getInt(ITEM_UPGRADE_INDEX, 0);
	}

	public static void saveUpgradeIndex(Context c, int index) {
		SharedPreferences sp = get(c);
		sp.edit().putInt(ITEM_UPGRADE_INDEX, index).commit();
	}

	public static String getUpgradeNotifyNoon(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_NOTIFY_NOON, "");
	}

	public static void saveUpgradeNotifyNoon(Context c, String date) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_NOTIFY_NOON, date).commit();
	}

	public static String getUpgradeNotifyNight(Context c) {
		SharedPreferences p = get(c);
		return p.getString(ITEM_UPGRADE_NOTIFY_NIGHT, "");
	}

	public static void saveUpgradeNotifyNight(Context c, String date) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_UPGRADE_NOTIFY_NIGHT, date).commit();
	}

	public static void saveSearchShortCutFlag(Context c, boolean flag) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(ITEM_SEARCHSHORTCUT_FLAG, flag).commit();
	}

	public static boolean getSearchShortCutFlag(Context c) {
		SharedPreferences sp = get(c);
		return sp.getBoolean(ITEM_SEARCHSHORTCUT_FLAG, false);
	}

	public static void saveRequestAppUpdateTime(Context c, String date) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_APPUPDATE_DATE, date).commit();
	}

	public static String getRequestAppupdateTime(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_APPUPDATE_DATE, "");
	}

	public static long getAppUpdateNextQueryTime(Context c) {
		SharedPreferences sp = get(c);
		return sp.getLong(ITEM_APPUPDATE_NEXT_QUERY_DATE, 0);
	}

	public static void saveAppUpdateNextQueryTime(Context c, long date) {
		SharedPreferences sp = get(c);
		sp.edit().putLong(ITEM_APPUPDATE_NEXT_QUERY_DATE, date).commit();
	}

	public static void saveAppUpdateNotificationNoon(Context c, String date) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_APPUPDATE_NOTIFICATION_NOON, date).commit();
	}

	public static String getAppUpdateNotificationNoon(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_APPUPDATE_NOTIFICATION_NOON, "");
	}

	public static void saveAppUpdateNotificationNight(Context c, String date) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_APPUPDATE_NOTIFICATION_NIGHT, date).commit();
	}

	public static String getAppUpdateNotificationNight(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_APPUPDATE_NOTIFICATION_NIGHT, "");
	}

	public static void saveAppUpdateIgnoreDialogFlag(Context c, boolean flag) {
		SharedPreferences sp = get(c);
		sp.edit().putBoolean(ITEM_SHOW_APP_UPDATE_IGNORE_DIALOG_FLAG, flag).commit();
	}

	public static boolean getAppUpdateIgnoreDialogFlag(Context c) {
		SharedPreferences sp = get(c);
		return sp.getBoolean(ITEM_SHOW_APP_UPDATE_IGNORE_DIALOG_FLAG, false);
	}

	public static int getActiviteCount(Context c) {
		SharedPreferences sp = get(c);
		return sp.getInt(ITEM_ACTIVITE_STATIC_COUNT, 0);
	}

	public static void saveUserId(Context c, String userId) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_USER_ID, userId).commit();
	}

	public static String getUserId(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_USER_ID, "");
	}

	public static void saveActiviteCount(Context c, int count) {
		SharedPreferences sp = get(c);
		sp.edit().putInt(ITEM_ACTIVITE_STATIC_COUNT, count).commit();
	}

	public static void saveUserPhoneNum(Context c, String phoneNum) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_USER_PHONE_NUM, phoneNum).commit();
	}

	public static String getUserPhoneNum(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_USER_PHONE_NUM, "");
	}

	public static void saveUserIconUrl(Context c, String iconUrl) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_USER_ICON_URL, iconUrl).commit();
	}

	public static String getUserIconUrl(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_USER_ICON_URL, "");
	}

	public static void saveUserNickName(Context c, String nickName) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_USER_NICK_NAME, nickName).commit();
	}

	public static String getUserNickName(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_USER_NICK_NAME, "");
	}

	public static void saveUserSex(Context c, String sex) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_USER_SEX, sex).commit();
	}

	public static String getUserSex(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_USER_SEX, "");
	}

	public static void saveUserBirthday(Context c, String birthday) {
		SharedPreferences sp = get(c);
		sp.edit().putString(ITEM_USER_BIRTHDAY, birthday).commit();
	}

	public static String getUserBirthday(Context c) {
		SharedPreferences sp = get(c);
		return sp.getString(ITEM_USER_BIRTHDAY, "");
	}

	///////////////////////////////////////////////////////////////////////////////
	// New settings below
	public static void setGuideVersion(Context context, int version) {
		if (context == null) {
			return;
		}

		SharedPreferences sp = context.getSharedPreferences("new_settings", Context.MODE_PRIVATE);
		sp.edit().putInt("guide_version", version).commit();
	}

	public static int getGuideVersion(Context context) {
		if (context == null) {
			return 0;
		}

		SharedPreferences sp = context.getSharedPreferences("new_settings", Context.MODE_PRIVATE);
		return sp.getInt("guide_version", 0);
	}

	public static void setLoadingAdRequestTime(Context context, long time) {
		if (context == null) {
			return;
		}

		SharedPreferences sp = context.getSharedPreferences("new_settings", Context.MODE_PRIVATE);
		sp.edit().putLong("loading_ad_time", time).commit();
	}

	public static long getLoadingAdRequestTime(Context context) {
		if (context == null) {
			return 0;
		}

		SharedPreferences sp = context.getSharedPreferences("new_settings", Context.MODE_PRIVATE);
		return sp.getLong("loading_ad_time", 0);
	}
}
